<?php
define('M_NOUSER',1);
include_once dirname(dirname(__FILE__)).'/include/general.inc.php';
if_siteclosed(1);
$_did = false;
if($enablestatic && !empty($static)){
	$mode = empty($mode) ? 'arc' : trim($mode);
	switch($mode){
		case 'arc':
			if(!$aid = max(0,intval(@$aid))) break;
			$addno = empty($addno) ? 0 : max(0,intval($addno));
			if(static_pause(@$arc_nostatic)) break;
			if(!$arr = $db->fetch_one("SELECT no_auto,needstatics FROM {$tblprefix}archives_sub WHERE aid='$aid'")) break;
			if($arr['no_auto'] || !($nss = $arr['needstatics'])) break;
			if(!($nss = explode(',',$nss)) || empty($nss[$addno]) || $nss[$addno] > $timestamp) break;
			include_once M_ROOT.'./include/common.fun.php';
			include_once M_ROOT.'./include/extends/arcedit.cls.php';
			$arc = new cls_arcedit;
			$arc->set_aid($aid,array('au'=>0));
			$arc->tostatic($addno);
			$_did = true;
		break;
		case 'cnindex':
			parse_str($_SERVER['QUERY_STRING'],$temparr);
			$cnstr = cnstr($temparr);
			$addno = max(0,intval(@$addno));
			if($cnstr){
				if(static_pause(@$cn_nostatic)) break;
				if(!$arr = $db->fetch_one("SELECT no_auto,needstatics FROM {$tblprefix}cnodes WHERE ename='$cnstr'")) break;
				if($arr['no_auto'] || !($nss = $arr['needstatics'])) break;
				if(!($nss = explode(',',$nss)) || empty($nss[$addno]) || $nss[$addno] > $timestamp) break;
			}else{
				if(static_pause(@$indexnostatic)) break;
				$k = $db->result_one("SELECT value FROM {$tblprefix}mconfigs WHERE varname='ineedstatic'");
				if(!$k || $k > $timestamp) break;
			}
			include_once M_ROOT.'./include/common.fun.php';
			include_once M_ROOT.'./include/static.fun.php';
			index_static($cnstr,$addno);
			$_did = true;
		break;
		case 'mcnode':
			parse_str($_SERVER['QUERY_STRING'],$temparr);
			$cnstr = mcnstr($temparr);
			$addno = max(0,intval(@$addno));
			if(static_pause(@$mcn_nostatic)) break;
			if($cnstr){
				if(!$arr = $db->fetch_one("SELECT no_auto,needstatics FROM {$tblprefix}mcnodes WHERE ename='$cnstr'")) break;
				if($arr['no_auto'] || !($nss = $arr['needstatics'])) break;
				if(!($nss = explode(',',$nss)) || empty($nss[$addno]) || $nss[$addno] > $timestamp) break;
			}else{
				$k = $db->result_one("SELECT value FROM {$tblprefix}mconfigs WHERE varname='mcnneedstatic'");
				if(!$k || $k > $timestamp) break;
			}				
			include_once M_ROOT.'./include/common.fun.php';
			include_once M_ROOT.'./include/static.fun.php';
			mindex_static($cnstr,$addno);
			$_did = true;
		break;
	}
}
if(!$_did && !empty($msp_static) && !empty($mspacecircle) && $mid = max(0,intval($mid))){
	$n = true;
	if(static_pause(@$ms_nostatic)) $n = false;
	if($n && !($arr = $db->fetch_one("SELECT mspacepath,msrefreshdate FROM {$tblprefix}members WHERE mid='$mid'"))) $n = false;
	if($n && (!$arr['mspacepath'] || ($timestamp - $arr['msrefreshdate'] < $mspacecircle * 60))) $n = false;
	if($n){
		include_once M_ROOT.'./include/common.fun.php';
		include_once M_ROOT.'./include/static.fun.php';
		mspace_static($mid);
		$_did = true;
	}
}
if(!empty($upsen)){
	define('M_UPSEN',1);
	$curuser->currentuser();
	$_infoed = 1;
}

if(!empty($enabelstat)){
	if(!empty($enable_mclick)){
		empty($_infoed) && $curuser->currentuser();
		if($memberid = $curuser->info['mid']){
			$str = 'msclicks=msclicks+1';
			if(empty($mclickscircle) || $curuser->info['msclicks'] >= $mclickscircle){
				$db->query("UPDATE {$tblprefix}members SET clicks=clicks+".($curuser->info['msclicks'] + 1)." WHERE mid='$memberid'");
				$str = "msclicks='0'";
			}
			$db->query("UPDATE {$tblprefix}msession SET $str WHERE msid='".$curuser->info['msid']."'");
		}
	}
	if(!empty($aid)){
		if($clickscachetime){
			$cf = M_ROOT.'./dynamic/stats/aclicks.cac';
			$ct = M_ROOT.'./dynamic/stats/aclicks_time.cac';
			mmkdir(M_ROOT.'./dynamic/stats/',0);
			if(!$_did && ($timestamp - @filemtime($ct) > $clickscachetime)){
				if(@$clicksarr = file($cf)){
					if(@$fp = fopen($cf,'w')) fclose($fp);
					if(@$fp = fopen($ct,'w')) fclose($fp);
					$clicksarr = array_count_values($clicksarr);
					foreach($clicksarr as $k => $v){
						if($k && $v){
							$db->query("UPDATE {$tblprefix}".atbl($k,2)." SET clicks=clicks+$v".($statweekmonth ? ",wclicks=wclicks+$v,mclicks=mclicks+$v" : '')." WHERE aid=$k");
						}
					}
					$_did = true;
				}
			}
			if(@$fp = fopen($cf,'a')){
				fwrite($fp,"$aid\n");
				fclose($fp);
			}
		}else $db->query("UPDATE {$tblprefix}".atbl($aid,2)." SET clicks=clicks+1".($statweekmonth ? ",wclicks=wclicks+1,mclicks=mclicks+1" : '')." WHERE aid='$aid'");
	}
	if(!empty($mid)){
		if($clickscachetime){
			$cf = M_ROOT.'./dynamic/stats/msclicks.cac';
			$ct = M_ROOT.'./dynamic/stats/msclicks_time.cac';
			mmkdir(M_ROOT.'./dynamic/stats/',0);
			if(!$_did && ($timestamp - @filemtime($ct) > $clickscachetime)){
				if($clicksarr = @file($cf)){
					if(@$fp = fopen($cf,'w')) fclose($fp);
					if(@$fp = fopen($ct,'w')) fclose($fp);
					$clicksarr = array_count_values($clicksarr);
					foreach($clicksarr as $k => $v) $k && $v && $db->query("UPDATE {$tblprefix}members_sub SET msclicks=msclicks+$v WHERE mid='$k'");
					$_did = true;
				}
			}
			if(@$fp = fopen($cf,'a')){
				fwrite($fp,"$mid\n");
				fclose($fp);
			}
		}else $db->query("UPDATE {$tblprefix}members_sub SET msclicks=msclicks+1 WHERE mid='$mid'");
	}
}
if(!$_did){
	include_once M_ROOT.'./include/cron.cls.php';
	$objcron=new cron;
	$objcron->_init_cron();
}
exit();

?>
